package com.sui.bigdata.flink.sql.function.util;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;

/**
 * 风控AES加密
 */
public class RiskAES {

    /**
     * 约定秘钥
     */
    private static final String ENCRYPT_USER_SALT = "&*($eIa61M$(&^%M";

    private static final String Algorithm = "AES";

    /**
     * 加密并且base64编码
     *
     * @param encodeParam
     * @param pwd
     * @return
     */
    public static String encryptAndBase64(String encodeParam, String pwd) {
        if (encodeParam != null && !"".equals(encodeParam)) {
            try {
                byte[] byteFina = interEncrypt(encodeParam.getBytes("UTF-8"), pwd);
                return InfrasBase64.getEncoder().encodeToString(byteFina);
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
        } else {
            return "";
        }

    }

    /**
     * base64解码之后解密
     *
     * @param decodeParam 密文
     * @param pwd         salt
     * @return 明文
     */
    public static String base64AndDecrypt(String decodeParam, String pwd) {
        byte[] bytes = InfrasBase64.getDecoder().decode(decodeParam);
        try {
            return new String(interDecode(bytes, pwd), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("解密失败！" + e);
        }
    }


    private static byte[] interEncrypt(byte[] byteS, String pwd) {
        byte[] byteFina = null;
        Cipher cipher;
        try {
            cipher = Cipher.getInstance(Algorithm);
            SecretKeySpec keySpec = new SecretKeySpec(getKey(pwd), "AES");
            cipher.init(Cipher.ENCRYPT_MODE, keySpec);
            byteFina = cipher.doFinal(byteS);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            cipher = null;
        }
        return byteFina;
    }


    private static byte[] interDecode(byte[] bytes, String pwd) {
        Cipher cipher;
        byte[] byteFina = null;
        try {
            cipher = Cipher.getInstance(Algorithm);
            SecretKeySpec keySpec = new SecretKeySpec(getKey(pwd), "AES");
            cipher.init(Cipher.DECRYPT_MODE, keySpec);
            byteFina = cipher.doFinal(bytes);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            cipher = null;
        }
        return byteFina;
    }

    private static byte[] getKey(String password) throws UnsupportedEncodingException {
        // 使用256位密码
        if (password.length() > 16) {
            password = password.substring(0, 16);
        } else if (password.length() < 16) {
            int count = (16 - password.length());
            for (int i = 0; i < count; i++) {
                password += "0";
            }
        }
        return password.getBytes("UTF-8");
    }

    public static void main(String[] args) {
        String planText = "N4EplBb3dCvpm+Wwe8yYWg==";
        String planText2 = "測試jiami!@#/r/n11";
        String planText3 = "陈泽伟";

        System.out.println("解密 " + base64AndDecrypt(planText, ENCRYPT_USER_SALT));
        System.out.println("加密后 " + encryptAndBase64(planText2, ENCRYPT_USER_SALT));
        System.out.println("加密后 " + encryptAndBase64(planText3, ENCRYPT_USER_SALT));
        System.out.println("加解密后 " + base64AndDecrypt(encryptAndBase64(planText2, ENCRYPT_USER_SALT), ENCRYPT_USER_SALT));

    }
}
